package com.work.biz.service.impl;

import java.util.List;

import com.work.common.utils.DateUtils;
import com.work.sys.domain.SysUserRole;
import com.work.sys.mapper.SysUserMapper;
import com.work.sys.mapper.SysUserRoleMapper;
import com.work.sys.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.work.biz.mapper.BizTeacherMapper;
import com.work.biz.domain.BizTeacher;
import com.work.biz.service.IBizTeacherService;

/**
 * 教师Service业务层处理
 *
 * @author lee
 * @date 2021-04-22
 */
@Service
public class BizTeacherServiceImpl implements IBizTeacherService {
    @Autowired
    private BizTeacherMapper bizTeacherMapper;
    @Autowired
    private SysUserMapper userInfoMapper;
    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private SysUserRoleMapper userRoleMapper;

    /**
     * 查询教师
     *
     * @param teacherId 教师ID
     * @return 教师
     */
    @Override
    public BizTeacher selectBizTeacherById(Long teacherId) {
        return bizTeacherMapper.selectBizTeacherById(teacherId);
    }

    /**
     * 查询教师列表
     *
     * @param bizTeacher 教师
     * @return 教师
     */
    @Override
    public List<BizTeacher> selectBizTeacherList(BizTeacher bizTeacher) {
        return bizTeacherMapper.selectBizTeacherList(bizTeacher);
    }

    /**
     * 新增教师
     *
     * @param bizTeacher 教师
     * @return 结果
     */
    @Override
    public int insertBizTeacher(BizTeacher bizTeacher) {
        bizTeacher.setCreateTime(bizTeacher.getCreateTime() == null ? DateUtils.getNowDate() : bizTeacher.getCreateTime());
        bizTeacher.setCreateBy(bizTeacher.getCreateBy() == null ? userInfoMapper.selectSysUserById(bizTeacher.getUserId()).getNickName() : bizTeacher.getCreateBy());
        SysUserRole userRole = new SysUserRole();
        userRole.setRoleId(3L);
        userRole.setUserId(bizTeacher.getUserId());
        List<SysUserRole> userRoles = userRoleMapper.selectUserRoleList(userRole);
        if (userRoles.size() <= 0) {
            roleService.insertUserRole(userRole);
        }
        return bizTeacherMapper.insertBizTeacher(bizTeacher);
    }

    /**
     * 修改教师
     *
     * @param bizTeacher 教师
     * @return 结果
     */
    @Override
    public int updateBizTeacher(BizTeacher bizTeacher) {
        bizTeacher.setUpdateTime(DateUtils.getNowDate());
        return bizTeacherMapper.updateBizTeacher(bizTeacher);
    }

    /**
     * 批量删除教师
     *
     * @param teacherIds 需要删除的教师ID
     * @return 结果
     */
    @Override
    public int deleteBizTeacherByIds(Long[] teacherIds) {
        return bizTeacherMapper.deleteBizTeacherByIds(teacherIds);
    }

    /**
     * 删除教师信息
     *
     * @param teacherId 教师ID
     * @return 结果
     */
    @Override
    public int deleteBizTeacherById(Long teacherId) {
        return bizTeacherMapper.deleteBizTeacherById(teacherId);
    }
}
